package com.thirdlib.zxing.activity;

import java.util.Vector;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.curry.orangelife.mobile.R;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.thirdlib.zxing.camera.CameraManager;
import com.thirdlib.zxing.decoding.CaptureActivityHandler;
import com.thirdlib.zxing.decoding.InactivityTimer;
import com.thirdlib.zxing.view.ViewfinderView;
/**
 * 二维码扫描
 * @author Administrator
 *
 */
public class QRCodeScanActivity extends OrangeLifeBaseActivity implements Callback{
	private boolean hasSurface;
    private CaptureActivityHandler handler;
    private ViewfinderView viewfinderView;
    private InactivityTimer inactivityTimer;
    private Vector<BarcodeFormat> decodeFormats;
    private String characterSet;
    private boolean playBeep;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.zxing_activity_qr_code_scan);
    	//初始化CameraManager,以便可以调用到摄像头包里的方法
    	CameraManager.init(getApplication());
    	viewfinderView = (ViewfinderView)findViewById(R.id.viewfinder_view);
    	Button mButtonBack = (Button)findViewById(R.id.btnBack);
    	mButtonBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
               QRCodeScanActivity.this.finish();				
			}
		});
    	hasSurface = false;
    	inactivityTimer = new InactivityTimer(this);
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	SurfaceView surfaceView = (SurfaceView)findViewById(R.id.preview_view);
    	SurfaceHolder surfaceHolder = surfaceView.getHolder();
    	if(hasSurface){
    	   initCamera(surfaceHolder);
    	}else{
    		surfaceHolder.addCallback(this);
    		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    	}
    	decodeFormats = null;
    	characterSet = null;
    }

    @Override
    protected void onPause() {
    	super.onPause();
    	if(handler!=null){
    		handler.quitSynchronously();
    		handler = null;
    	}
    	CameraManager.get().closeDriver();
    }
    
    @Override
    protected void onDestroy() {
    	inactivityTimer.shutdown();
    	super.onDestroy();
    }
	private void initCamera(SurfaceHolder surfaceHolder) {
        try {
			CameraManager.get().openDriver(surfaceHolder);
		} catch (Exception e) {
			e.printStackTrace();
		}	
        if(handler == null){
        	handler = new CaptureActivityHandler(this, decodeFormats, characterSet);
        } 
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		if(!hasSurface){
			hasSurface = true;
			initCamera(holder);
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
         hasSurface = false;		
	}
    
    public ViewfinderView getViewfinderView(){
    	return viewfinderView;
    }
    
    public void drawViewfinder(){
    	viewfinderView.drawViewfinder();
    }
    
    public Handler getHandler(){
    	return handler;
    }
    /**
     * 处理扫描结果
     */
    public void handleDecode(Result result,Bitmap barcode){
    	inactivityTimer.onActivity();
    	String resultString = result.getText();
    	if(resultString.equals("")){
    		Toast.makeText(QRCodeScanActivity.this, "Scan failed!",
					Toast.LENGTH_SHORT).show();
    	}else{//扫描结束
    		Intent resultIntent = new Intent();
    		Bundle bundle = new Bundle();
    		bundle.putString("result", resultString);
    		resultIntent.putExtras(bundle);
    		this.setResult(RESULT_OK,resultIntent);
    	}
    	finish();
    }
}
